package com.baofu.paycore.service;

import com.baofu.paycore.biz.model.BackPayBO;
import com.baofu.paycore.biz.paycore.handler.impl.PreAuthConfirmHandler;
import com.baofu.paycore.common.utils.ExceptionUtil;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.storer.PreAuthRepository;
import com.baofu.paycore.service.converter.BO2DTOConverter;
import com.baofu.paycore.service.facade.AuthConfirmServiceFacade;
import com.baofu.paycore.service.facade.model.CancelResDTO;
import com.baofu.paycore.service.facade.model.ReverseResDTO;
import com.baofu.paycore.service.facade.model.authconfirm.AuthConfirmCancelDTO;
import com.baofu.paycore.service.facade.model.authconfirm.AuthConfirmDTO;
import com.baofu.paycore.service.facade.model.authconfirm.AuthConfirmResDTO;
import com.baofu.paycore.service.facade.model.authconfirm.AuthConfirmReverseDTO;
import com.baofu.paycore.service.factory.impl.PreAuthConfirmFactoryImpl;
import com.baofu.paycore.service.validate.impl.preAuth.PreAuthConfirmValidate;
import com.system.commons.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 预授权确认相关服务
 * <p>
 *  1.预授权完成接口
 *  2.预授权完成撤销接口
 *  3.预授权完成冲正接口
 *  4.预授权完成撤销冲正接口
 * </p>
 * User: xin deng  Date: 16/8/31 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class AuthConfirmService implements AuthConfirmServiceFacade {

    /**
     * 预授权确认校验
     */
    @Autowired
    private PreAuthConfirmValidate preAuthConfirmValidate;

    /**
     * 预授权确认工厂服务
     */
    @Autowired
    private PreAuthConfirmFactoryImpl payBaseFactory;

    /**
     * 预授权处理服务
     */
    @Autowired
    private PreAuthRepository preAuthRepository;

    /**
     * 预授权确认服务
     */
    @Autowired
    private PreAuthConfirmHandler preAuthConfirmHandler;

    /**
     * 预授权完成接口
     *
     * @param authConfirmDTO 预授权完成请求参数
     * @param transLogId     日志id
     * @return 预授权完成响应
     * response.isSuccess()标示方法的远程调用状态，方法实际执行结果从result 取值
     */
    @Override
    public Result<AuthConfirmResDTO> authConfirm(AuthConfirmDTO authConfirmDTO, String transLogId) {
        Result<AuthConfirmResDTO> result = null;
        boolean flag = false;

        try {
            MDC.put(SystemMarker.TRACE_LOG_ID, transLogId);

            log.info("预授权完成接口 请求参数：{}", authConfirmDTO);

            flag = preAuthConfirmValidate.lock(authConfirmDTO);

            //1、请求对象参数校验
            preAuthConfirmValidate.paramsValidate(authConfirmDTO);

            PreAuthBO authBO = preAuthConfirmValidate.validateUniqueOrder(authConfirmDTO, preAuthRepository);

            //2、请求对象转换
            PreAuthBO preAuthBO = payBaseFactory.build(authConfirmDTO, authBO);
            preAuthBO.setTradeLogId(transLogId);

            //3、消费相关信息落地
            preAuthRepository.addOrder(preAuthBO);

            //4、业务流程处理
            BackPayBO backPayBO = preAuthConfirmHandler.process(preAuthBO);

            //5、接口返回对象生成
            AuthConfirmResDTO authConfirmResDTO = BO2DTOConverter.getAuthorizeConfirmResDTO(backPayBO);
            result = new Result<>(authConfirmResDTO);

        } catch (Exception e) {

            log.error("预授权完成接口 异常 EXCEPTION:{}", e);
            result = ExceptionUtil.doExceptionService(e, AuthConfirmResDTO.class);
        } finally {

            log.info("预授权完成接口 响应:{}",result);
            preAuthConfirmValidate.release(authConfirmDTO,flag);
            MDC.clear();
        }
        return result;
    }

    /**
     * 预授权完成撤销接口
     *
     * @param authConfirmCancelDTO 预授权完成撤销请求参数
     * @param transLogId           日志id
     * @return 预授权完成撤销响应
     * response.isSuccess()标示方法的远程调用状态，方法实际执行结果从result 取值
     */
    @Override
    public Result<CancelResDTO> authConfirmCancel(AuthConfirmCancelDTO authConfirmCancelDTO, String transLogId) {
        return null;
    }

    /**
     * 预授权完成冲正接口
     *
     * @param reverseDTO 预授权完成冲正请求参数
     * @param transLogId 日志id
     * @return 预授权完成冲正响应
     * response.isSuccess()标示方法的远程调用状态，方法实际执行结果从result 取值
     */
    @Override
    public Result<ReverseResDTO> authConfirmReverse(AuthConfirmReverseDTO reverseDTO, String transLogId) {
        return null;
    }

    /**
     * 预授权完成撤销冲正接口
     *
     * @param authConfirmCancelDTO 预授权完成撤销冲正请求参数
     * @param transLogId           日志id
     * @return 预授权完成撤销冲正响应
     * response.isSuccess()标示方法的远程调用状态，方法实际执行结果从result 取值
     */
    @Override
    public Result<CancelResDTO> cancelAuthConfirmReverse(AuthConfirmCancelDTO authConfirmCancelDTO, String transLogId) {
        return null;
    }
}
